package work.xiaohong.saolei.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import work.xiaohong.model.Result;
import work.xiaohong.model.StatusCode;
import work.xiaohong.saolei.pojo.Grade;
import work.xiaohong.saolei.pojo.User;
import work.xiaohong.saolei.service.SaoleiGradeService;
import work.xiaohong.saolei.service.SaoleiUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Tag(name = "SaoleiGrade接口文档")
@RestController
@RequestMapping("/saoleiGrade")
public class SaoleiGradeController {

    @Autowired
    private SaoleiGradeService saoleiGradeService;

    @Autowired
    private SaoleiUserService saoleiUserService;

    /***
     * 分页条件搜索实现SaoleiGrade
     * @param saoleiGrade
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询SaoleiGrade",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "SaoleiGrade对象",required = true) Grade saoleiGrade, @PathVariable int page, @PathVariable  int size){
        //调用SaoleiGradeService实现分页条件查询SaoleiGrade
        Page pageInfo = saoleiGradeService.findPage(saoleiGrade, page, size);
        return new Result<Page>(true, StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 分页搜索实现SaoleiGrade
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询SaoleiGrade",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SaoleiGradeService实现分页查询SaoleiGrade
        Page pageInfo = saoleiGradeService.findPage(page, size);
        return new Result<Page>(true, StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 多条件搜索SaoleiGrade数据
     * @param saoleiGrade
     * @return
     */
    @Operation(summary = "多条件查询SaoleiGrade",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping(value = "/search" )
    public Result<List<Grade>> findList(@RequestBody(required = false) @Parameter(name = "SaoleiGrade对象",required = true) Grade saoleiGrade){
        //调用SaoleiGradeService实现条件查询SaoleiGrade
        List<Grade> list = saoleiGradeService.findList(saoleiGrade);
        return new Result<List<Grade>>(true,StatusCode.OK.getCode(),"查询成功",list);
    }

    /***
     * 根据Id删除saoleiGrade数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除SaoleiGrade",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用SaoleiGradeService实现根据ID删除
        saoleiGradeService.delete(id);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }

    /***
     * 根据ID修改SaoleiGrade数据
     * @param saoleiGrade
     * @return
     */
    @Operation(summary = "根据ID修改SaoleiGrade",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PutMapping
    public Result update(@RequestBody @Parameter(name = "SaoleiGrade对象",required = true) Grade saoleiGrade){
        //调用SaoleiGradeService实现修改SaoleiGrade
        saoleiGradeService.update(saoleiGrade);
        return new Result(true,StatusCode.OK.getCode(),"修改成功");
    }

    /***
     * 添加成绩
     * @param saoleiGrade
     * @return
     */
    @Operation(summary = "添加成绩",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping
    @Transactional
    public Result add(@RequestBody  @Parameter(name = "SaoleiGrade对象",required = true) Grade saoleiGrade){
        // 查询玩家最好成绩
        User user = saoleiUserService.findById(saoleiGrade.getPlayer());

        if (user == null){
            return new Result(true,StatusCode.ERROR.getCode(),"没有该用户信息");
        }

        // 加积分
        switch (saoleiGrade.getModel()){
            case "8x8":
                user.setCount(user.getCount()+8);
                break;
            case "10x10":
                user.setCount(user.getCount()+10);
                break;
            case "12x12":
                user.setCount(user.getCount()+12);
                break;
            case "15x15":
                user.setCount(user.getCount()+15);
                break;
            case "18x18":
                user.setCount(user.getCount()+18);
                break;
            case "20x20":
                user.setCount(user.getCount()+20);
                break;
            case "25x125":
                user.setCount(user.getCount()+25);
                break;
            default:
                user.setCount(user.getCount()+5);
                break;
        }


        String bestGrade = user.getBestGrade();

        if (!StringUtils.isEmpty(bestGrade)){
            // 转为对象
            List<Grade> gradeList = JSONObject.parseArray(bestGrade, Grade.class);

            //找到对应模式成绩
            Boolean flag = true;
            for (int i = 0; i < gradeList.size(); i++) {
                if (gradeList.get(i).getModel().equals(saoleiGrade.getModel())){
                    // 有该模式记录
                    flag = false;
                    // 判断成绩
                    if(gradeList.get(i).getGrade() >= saoleiGrade.getGrade()){
                        // 替换最好成绩
                        gradeList.set(i,saoleiGrade);
                    }
                }
            }

            if (flag){
                // 没有该模式 直接添加
                gradeList.add(saoleiGrade);
            }

            //转为字符串添加
            user.setBestGrade(JSON.toJSONString(gradeList));
            // 总场次加1
            user.setGameTotal(user.getGameTotal()+1);
            // 更新用户信息
            saoleiUserService.updateById(user);
        }else {
            // 直接添加
            List<Grade> gradeList = new ArrayList<>();
            gradeList.add(saoleiGrade);
            //转为字符串添加
            user.setBestGrade(JSON.toJSONString(gradeList));
            // 总场次加1
            user.setGameTotal(user.getGameTotal()+1);
            // 更新用户信息
            saoleiUserService.updateById(user);
        }

        //调用SaoleiGradeService实现添加SaoleiGrade
        saoleiGradeService.add(saoleiGrade);
        return new Result(true,StatusCode.OK.getCode(),"保存成绩成功");
    }

    /***
     * 根据ID查询SaoleiGrade数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询SaoleiGrade",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
    @GetMapping("/{id}")
    public Result<Grade> findById(@PathVariable Long id){
        //调用SaoleiGradeService实现根据ID查询SaoleiGrade
        Grade saoleiGrade = saoleiGradeService.findById(id);
        return new Result<Grade>(true,StatusCode.OK.getCode(),"查询成功",saoleiGrade);
    }

    /***
     * 查询SaoleiGrade全部数据
     * @return
     */
    @Operation(summary = "查询所有SaoleiGrade",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @GetMapping
    public Result<List<Grade>> findAll(){
        //调用SaoleiGradeService实现查询所有SaoleiGrade
        List<Grade> list = saoleiGradeService.findAll();
        return new Result<List<Grade>>(true, StatusCode.OK.getCode(),"查询成功",list) ;
    }
}
